/**
 * Created by Administrator on 2018/7/9.
 */

/**
 * 前台js常用函数工具类
 * @author:chenyi
 * @version 1.0
 * @Date: 2017/12/8
 */
(function ($, window) {
    //前台工具类对象
    window.$f = window.$f || {};

    /**layui数据表格列显示与隐藏*/
    window.$f.changeCols = function (obj,tableId) {

        var _table = $(obj).parents(".layui-table-view");
        var th = _table.find(".layui-table-header th");
        var _cols = "";
        for (var i = 0; i < th.length; i++) {
            var _this = $(th[i]);
            var _name = _this.attr("data-field");
            if (_name === "0" || _name === "bar") {
                continue
            };
            var _title = _this.text();
            //获取所有列

            var _col = '<input type="checkbox" name="' + _name + '" title="' + _title + '" lay-filter="ChangeCols" lay-skin="primary" >';
            if (_this.css("display") !== "none") {
                _col = _col.replace('<input type="checkbox" ', '<input type="checkbox" checked ');
            }
            _cols += _col + "<br>";
        }
        /*打开面板*/
        layer.open({
            title: false,
            type: 1,
            area: ['300px', '520px'],
            fixed: false, //不固定
            resize: false,
            maxmin: false,
            // shade:false,
            content: '<div class="layui-form cy-cols">' + _cols + '</div>',
            success: function (layero, index) {
                layui.use(['form', 'table'], function () {
                    var form = layui.form;
                    var table = layui.table;
                    form.render('checkbox');
                    form.on('checkbox(ChangeCols)', function (data) {
                        //当前操作的字段
                        var _name = $(data.elem).attr("name") || "";
                        //获取表格当前配置
                        var options = table.get("accountTable");
                        //如果是勾选
                        if (data.elem.checked === true) {
                            for (var i = 0; i < options.cols[0].length; i++) {
                                if (options.cols[0][i].field === _name) {
                                    options.cols[0][i].hide = false;
                                    break;
                                }
                            }
                        } else {
                            for (var i = 0; i < options.cols[0].length; i++) {
                                if (options.cols[0][i].field === _name) {
                                    options.cols[0][i].hide = true;
                                    break;
                                }
                            }
                        }
                        //重置表格配置并渲染
                        table.set(options);
                        table.reload(tableId, {});

                    });
                });
            }
        });

    }

})(jQuery, window);